import dayjs from "dayjs"
import Schema from 'validate'
const repairInfo = new Schema(
  {
    // 房屋id
    houseId: {
      type: String, // 类型
      required: true, // 非空
      message: '维修房屋不能为空', // 提示信息
    },
    // 房屋id
    repairItemId: {
      type: String, // 类型
      required: true, // 非空
      message: '维修项目不能为空', // 提示信息
    },
    // 手机号码
    mobile: {
      type: String, // 类型
      match: /^1[3-9]\d{9}$/, // 手机号码
      message: '手机不合法', // 提示信息
    },
    // 预约日期
    appointment: {
      type: String, // 类型
      required: true, // 非空
      message: '预约日期不能为空', // 提示信息
    },
    // 问题描述
    description: {
      type: String, // 类型
      length: { min: 1, max: 200 },
      message: '问题描述1-200字', // 提示信息
    },
    // 附件列表
    attachment: {
      type: Array, // 数组类型
      // use 要接收一个对象，对象中 放一个函数，函数就是我们自定义的验证规则
      use: {
        // 函数名称自定义
        // v 稍后要验证的字段的值  此时它表示  附件列表数组
        // 函数必须返回 true或者false，   如果验证通过，返回true ， 如果验证不通过，返回false
        listLengthValidate(v) {
          return !!v.length
        },
      },
      message: '问题附件不能为空',
    },
  },

  { strip: false } // 解决 验证对象后，数据丢失属性的问题！！
)
Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    houseId: '',
    houseInfo: '',
    appointment: '',
    mobile: '',
    attachment: [],
    description: '',
    repairItemId: ''
  },
  onLoad({ id }) {
    if (id) {
      wx.setNavigationBarTitle({ title: '修改保修信息' })
      this.getRepaiInfo(id)
    } else {
      wx.setNavigationBarTitle({ title: '填写保修信息' })
    }
    this.getHouseList()
    this.getRepairItems()
  },
  async getRepaiInfo(id) {
    const res = await wx.http({
      url: '/repair/' + id
    })
    const { ...data } = res.data
    this.setData(data)
  },
  async getHouseList() {
    const res = await wx.http({ url: '/house' })
    this.setData({
      houseList: res.data
    })
  },
  async getRepairItems() {
    const res = await wx.http({ url: '/repairItem' })
    this.setData({ repairItem: res.data })
  },
  onHouseSelect(ev) {
    this.setData({
      houseId: ev.detail.id,
      houseInfo: ev.detail.name
    })
  },
  onRepairItemSelect(ev) {
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  onDateTimeConfirm(ev) {
    this, this.setData({
      appointment: dayjs(ev.detail).format('YYYY-MM-DD'),
      dateLayerVisible: false
    })
  },
  onDateTimeCancel() {
    this, this.setData({
      dateLayerVisible: false
    })
  },
  onAfterRead(ev) {
    const thumb = ev.detail.file.thumb
    console.log(thumb);
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: thumb,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`,
      },
      success: (res) => {
        const data = JSON.parse(res.data)
        const { attachment } = this.data
        attachment.push(data.data)
        this.setData({ attachment })
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },
  onDelete(ev) {
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗',
      success: ({ confirm }) => {
        if (confirm) {
          const index = ev.detail.index
          const { attachment } = this.data
          attachment.splice(index, 1)
          this.setData({ attachment })
        }
      }
    })
  },
  async onSubmin() {
    const { id, appointment, attachment, description, houseId, mobile, repairItemId } = this.data
    const params = { id, appointment, attachment, description, houseId, mobile, repairItemId }
    if (!params.id) {
      delete params.id
    }
    const errors = repairInfo.validate(params)
    console.log(errors);
    if (errors.length) {
      return wx.utils.toast(errors[0].message)
    }
    const res = await wx.http({
      url: '/repair',
      method: 'post',
      data: params
    })
    if (res.code !== 10000) {
      return wx.utils.toast(res.message)
    }
    wx.showToast({ title: '操作成功' })
    setTimeout(() => {
      wx.navigateTo({ url: '/repair_pkg/pages/list/index' })
    }, 1500)
  },
})
